package com.linkage.jz.bankruptcy.net;


import android.util.Log;


import com.google.gson.JsonObject;

import java.net.SocketTimeoutException;

import rx.Subscriber;

/**
 * 基础的Subscriber，主要是统一处理onError中的异常情况
 */
public class BaseSubscriber<T> extends Subscriber<T> {
    private final static String TAG = "BaseSubscriber";

    @Override
    public void onCompleted() {
        Log.v(TAG, "---->onCompleted");
    }

    @Override
    public void onError(Throwable error) {
        Log.e(TAG, "---->onError");
        error.printStackTrace();
        if (error instanceof SocketTimeoutException) {
            ToastUtil.showShort("连接超时，请检查您的网络情况");
        } else {
            if(error !=null) {
                String str = error.getMessage();
//                ToastUtil.showShort("请求失败:"+str );
            }

        }
    }

    @Override
    public void onNext(T t) {
        Log.v(TAG, "---->onNext");
        if (t instanceof JsonObject) {
            int code = ((JsonObject) t).get("code").getAsInt();
            if (code != 0) {
                String msg = ((JsonObject) t).get("msg").getAsString();
                ToastUtil.showShort(msg);
            }
        }
    }
}
